﻿//-----------------------------------------------------------------
//
// @(#)$Id: Certificate.aspx.cs,v 1.0 2013/12/13 17:53:53 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:53:53 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Web;

public partial class Certificate : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = Session["OrderPingZheng"].ToString();

        string sql = string.Format(@"select distinct DispatchDate,AddressLine1,PostalCode
from ParcelPrint as a
where OrderLineItemId='{0}'", str);

        DataSet ds = new DataSet();
        ds = DataSelectCenterDB(sql, "ParcelPrint");

        string time = "";
        string address1 = "";
        string postcode = "";
        string suijishu = "";

        if (ds.Tables["ParcelPrint"].Rows.Count > 0)
        {
            time = Convert.ToDateTime(ds.Tables["ParcelPrint"].Rows[0]["DispatchDate"]).ToString("dd/MM/yyyy HH:mm");
            address1 = Convert.ToString(ds.Tables["ParcelPrint"].Rows[0]["AddressLine1"]);
            postcode = Convert.ToString(ds.Tables["ParcelPrint"].Rows[0]["PostalCode"]);
            Random rd = new Random();
            suijishu = Convert.ToString(rd.Next(1, 9)) + "-" + Convert.ToString(rd.Next(111111, 999999));
        }

        Bitmap bitmap = new Bitmap(500, 900);
        Graphics g = Graphics.FromImage(bitmap);
        g.Save();
        g.Dispose();
        bitmap.MakeTransparent(Color.Transparent);

        Bitmap vBitmapBack = new Bitmap(HttpContext.Current.Server.MapPath("../../Images/back.png"));

        Bitmap vBitmapFace = (Bitmap)AddShuiYinWord(bitmap, string.Format(@"








            Post Office Ltd.
          WWW.postoffice.co.uk
		   
{0}       TP:02    BP:02    SU:KL1
Carr Street                         FAD:1251309
17 Carr Street
Ipswich
Suffolk
IP4 1ER

Session Id: {1}
Txn Id: 1

Horizon Certificate of Posting

An item addressed as follows:-

Building Name or No: {2}
PostCode/Zip Code: {3}

Has been accepted here today.

   It is important that you retain this
  receipt as it is your Proof Of Posting.
      
     Please refer to Separate Terms and
                Conditions.


For information regarding Royal Mail
products and services visit our web site
at WWW.postoffice.co.uk

      Withdraw cash over the counter
         from your bank account
  at any of our branches(selected banks)
           It's free and easy

      WWW.postoffice.co.uk/banking

	    This is not a VAT receipt", time, suijishu, address1, postcode));
        Graphics vGraphics = Graphics.FromImage(vBitmapBack);
        vGraphics.DrawImage(vBitmapFace, new Point(0, 0));

        vBitmapBack.Save(HttpContext.Current.Server.MapPath("../../Images/abc.png"), System.Drawing.Imaging.ImageFormat.Png);
        vBitmapBack.Dispose();
        Image1.ImageUrl = "../../Images/abc.png?random=" + DateTime.Now.Ticks;
    }

    public static DataSet DataSelectCenterDB(string sql, string table_name)
    {
        string strConnection = @"Data Source=192.168.1.194;Initial Catalog=CenterDB;Persist Security Info=True;User ID=sa;Password=kingdee1!";
        SqlConnection conn = new SqlConnection(strConnection);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sql, strConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, table_name);
        conn.Close();
        return ds;
    }

    private System.Drawing.Image AddShuiYinWord(Bitmap bmp, string text)
    {
        string addText = text;
        System.Drawing.Image image = bmp;
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(image, 0, 0, image.Width, image.Height);
        System.Drawing.Font f = new System.Drawing.Font("OCR A Extended", 12, FontStyle.Bold);
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Gray);

        g.DrawString(addText, f, b, 15, 15);
        g.Dispose();

        return image;
    }

    public static byte[] BitmapToBytes(Bitmap Bitmap)
    {
        MemoryStream ms = null;
        try
        {
            ms = new MemoryStream();
            Bitmap.Save(ms, Bitmap.RawFormat);
            byte[] byteImage = new Byte[ms.Length];
            byteImage = ms.ToArray();
            return byteImage;
        }
        catch (ArgumentNullException ex)
        {
            throw ex;
        }
        finally
        {
            ms.Close();
        }
    }
}